<?php

/**
 * Model_Banner
 * 
 * @package    Model
 * @author     Trọng HM
 * @version    0.1
 */
class Model_Banner extends Zend_Db_Table_Abstract
{
	protected $_name = "banner";
	
	/**
	 * 
	 * Lấy toàn bộ ảnh của slide
	 */
	public function getAllBanners(){
		return $this->fetchAll($this->select()->order('priority'));
	}
	/**
	 * 
	 * Lấy ảnh trong slide theo id
	 * @param unknown_type $id
	 */
	public function getBannerByID($id){
		return $this->find($id)->current();
	}
	/**
	 * 
	 * Tạo một ảnh mới trong slide
	 * @param string $name
	 * @param string $caption
	 * @param string $link
	 * @param int $priority
	 * @return int | boolean
	 */
	public function createBanner($src, $caption, $link, $priority){
		try{
			$banner = $this->createRow();
			$banner->src = $src;
			$banner->caption = $caption;
			$banner->link = $link;
			$banner->priority = $priority;
			$banner->save();
			$id = $this->_db->lastInsertId();
			if($id) return $id;
			else return false;
		}catch (Exception $ex){
			echo $ex->getMessage();
			return false;
		}
	}
	/**
	 * 
	 * Update thông tin ảnh trong slide
	 * @param int $id
	 * @param string $name
	 * @param string $caption
	 * @param string $link
	 * @param int $priority
	 * @return boolean
	 */
	public function updateBanner($id, $src, $caption, $link, $priority){
		try{
			$data = array("src" => $src, "caption" => $caption, "link" => $link, "priority" => $priority);
			if($this->update($data, "id = ".$id)) return true;
			else return false;
		}catch(Exception $ex){
			echo $ex->getMessage();
			return false;	
		}
	}
	/**
	 * 
	 * Xóa ảnh
	 * @param int $id
	 */
	function deleteBanners($id){
		try{
			if(is_numeric($id)){
				return $this->delete('id = '. $id );
			}else{
				return $this->delete('id in ('.$id.')');
			}
		}catch (Exception $ex){
			echo $ex->getMessage();
			return false;	
		}
	}
}
?>